package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.Result;
import com.example.lotterysystem.common.enums.GlobalErrorCodeEnum;
import com.example.lotterysystem.common.enums.UserIdentityEnum;
import com.example.lotterysystem.common.exception.GlobalException;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.PasswordLoginParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.param.VerifyCodeLoginParam;
import com.example.lotterysystem.controller.result.UserHistoryActivityResult;
import com.example.lotterysystem.controller.result.UserInfoResult;
import com.example.lotterysystem.controller.result.UserLoginResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.UserService;
import com.example.lotterysystem.service.VerifyCodeService;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 注册
     */
    @PostMapping("/register")
    public Result<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        // 打印日志
        log.info("userController:  UserRegisterParam: {}", JacksonUtil.writeValueAsString(param));
        // 调用 Service 层
        UserRegisterDTO registerDTO = userService.register(param);

        if (registerDTO == null){
            throw new GlobalException(GlobalErrorCodeEnum.REGISTER_ERROR);
        }
        UserRegisterResult result = new UserRegisterResult();
        result.setUserId(registerDTO.getUserId());
        return Result.success(result);
    }

    /**
     * 发送验证码服务
     * @param phoneNumber 手机号
     * @return
     */
    @RequestMapping("/verifyCode/send")
    public Result<Boolean> sendVerifyCode(String phoneNumber){
        log.info("userController: sendVerifyCode: {}", phoneNumber);
        verifyCodeService.sendVerifyCode(phoneNumber);
        return Result.success(Boolean.TRUE);
    }

    @RequestMapping("/login/password")
    public Result<UserLoginResult> loginByPassword(@RequestBody @Validated PasswordLoginParam param){
        log.info("userController: loginByPassword: {}",
                JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        if(userLoginDTO == null) {
            throw new GlobalException(GlobalErrorCodeEnum.LOGIN_ERROR);
        }
        UserLoginResult result = new UserLoginResult();
        result.setToken(userLoginDTO.getToken());
        result.setIdentity(userLoginDTO.getIdentity().name());
        result.setUserName(userLoginDTO.getUserName());
        return Result.success(result);
    }

    @RequestMapping("/login/verifyCode")
    public Result<UserLoginResult> loginByVerifyCode(@RequestBody @Validated VerifyCodeLoginParam param){
        log.info("userController: loginByVerifyCode: {}",
                JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        if(userLoginDTO == null) {
            throw new GlobalException(GlobalErrorCodeEnum.LOGIN_ERROR);
        }
        UserLoginResult result = new UserLoginResult();
        result.setToken(userLoginDTO.getToken());
        result.setIdentity(userLoginDTO.getIdentity().name());
        result.setUserName(userLoginDTO.getUserName());
        return Result.success(result);
    }


    @RequestMapping("/base-user/getUserInfoList")
    public Result<List<UserInfoResult>> getUserInfoList(HttpServletRequest request, String identity){
        log.info("userController: getUserInfoList: {}", identity);
        List<UserDTO> userList = userService.getUserInfoList(request, UserIdentityEnum.forName(identity));
        List<UserInfoResult> results = transfer(userList);
        return Result.success(results);
    }

    @RequestMapping("/user-history/getUserActivities")
    public Result<List<UserHistoryActivityResult>> getUserActivities(Long userId){
        log.info("userController: getUserActivities: {}", userId);
        List<UserHistoryActivityResult> results = userService.getUserActivities(userId);
        return Result.success(results);
    }
    private List<UserInfoResult> transfer(List<UserDTO> userList){
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }
        List<UserInfoResult> results = new ArrayList<>();
        for (UserDTO user : userList) {
            UserInfoResult userInfoResult = new UserInfoResult();
            userInfoResult.setUserId(user.getUserId());
            userInfoResult.setUserName(user.getUserName());
            userInfoResult.setIdentity(user.getIdentity().name());
            results.add(userInfoResult);
        }
        return results;
    }
}
